package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.Ads;
import ex.com.jm.food.entity.PicStyle;
import ex.com.jm.food.service.CompanyAdsService;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.JsonUtils;

/**
 * 
 * @author zhaojw
 *
 */
public class CompanyAdsAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private CompanyAdsService companyAdsService;
	private Ads ads;
	private List<PicStyle> picStyles=new ArrayList<PicStyle>();
    private String comId;
    private int initPage=1;
    private int pageRow=10;
    private int totalCount;
    private String adId;
    private String adName;
    private String order;
    private String sort;
    private String s_date;
    private String e_date;
    private Integer status;
    private Map<String, String> dateMap;
	public CompanyAdsService getCompanyAdsService() {
		return companyAdsService;
	}
	public void setCompanyAdsService(CompanyAdsService companyAdsService) {
		this.companyAdsService = companyAdsService;
	}
	public Ads getAds() {
		return ads;
	}
	public void setAds(Ads ads) {
		this.ads = ads;
	}
	public List<PicStyle> getPicStyles() {
		return picStyles;
	}
	public void setPicStyles(List<PicStyle> picStyles) {
		this.picStyles = picStyles;
	}
	public String getComId() {
		return comId;
	}
	public void setComId(String comId) {
		this.comId = comId;
	}
	public int getInitPage() {
		return initPage;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public String getAdId() {
		return adId;
	}
	public void setAdId(String adId) {
		this.adId = adId;
	}
	public String getAdName() {
		return adName;
	}
	public void setAdName(String adName) {
		this.adName = adName;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	
	public String getS_date() {
		return s_date;
	}
	public void setS_date(String s_date) {
		this.s_date = s_date;
	}
	public String getE_date() {
		return e_date;
	}
	public void setE_date(String e_date) {
		this.e_date = e_date;
	}
	public Integer getStatus() {
		return status;
	}
	public void setStatus(Integer status) {
		this.status = status;
	}
	public Map<String, String> getDateMap() {
		return dateMap;
	}
	public void setDateMap(Map<String, String> dateMap) {
		this.dateMap = dateMap;
	}
	/**
	 * 列表页面
	 * @return
	 */
	public String adsList(){
		dateMap=DateUtils.getDate();
		return "adsList";
	}
	
	public void getAdsList(){
		 HttpServletResponse response = ServletActionContext.getResponse();  
		 response.setContentType("text/plain;charset=utf-8");  
		 int offset=(initPage-1)*pageRow;
		 List<Ads> ads=this.getCompanyAdsService().getAds(offset, pageRow, comId, adName, status, s_date, e_date, order, sort);
		 JSONArray adsarry=JSONArray.fromObject(ads);
		 totalCount=this.getCompanyAdsService().adsCount(comId, adName, status, s_date, e_date);
		 JSONObject jo=JsonUtils.buildJson(adsarry, initPage, pageRow, totalCount);
		 PrintWriter out=null;
			try {
				out = response.getWriter();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(jo);
			out.print(jo);
			out.flush();
			out.close();
	}
	
	public String editAds(){
		picStyles=this.getCompanyAdsService().getPicStyles();
		if (adId!=null) {
			ads=this.getCompanyAdsService().editAds(adId);
		}
		return "edit";
		
	}
	
	public String saveAd(){
		if(ads.getAdImage()==null||ads.getAdImage().equals("")){
			addFieldError("ads.adImage", "图片不能为空");
			return "edit";
		}
		if(ads.getAdName()==null||ads.getAdName().equals("")){
			addFieldError("ads.adName", "图片不能为空");
			return "edit";
		}
		ads.setStatus(0);
		if(ads.getAdId()!=null&&!ads.getAdId().equals("")){
			this.getCompanyAdsService().updateAds(ads);
			return "list";
		}
		ads.setCreated(DateUtils.getCurrentDate());
		this.getCompanyAdsService().saveAds(ads);
		return "list";
	}
	
	public String delAd(){
		this.getCompanyAdsService().delAds(adId);
		return "list";
	}
}
